<?php
global $base_url; 
?>
<h3>Exported Order Files</h3>

<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>Filename</th>
            <th>Create Time</th>
            <th>Summary</th>
            <th>Action</th>
        </tr>
    </thead>
    <tbody>
        <?php if (count($data) != 0): ?>
            <?php foreach ($data as $row) : ?>
                <tr>
                    <?php $lines = explode(",", $row['summary']);
                    $arevorders;
                    $cartorders;
                    $totalamount;
                    $newtable = "<table><tr><td><b>Drive Code</b></td><td><b># of Orders</b></td><td><b># of Products</b></td>
                        <td><b>Total Gross($)</b></td><td><b>Processing Fee($)</b></td><td><b>Postage($)</b></td>
                        <td><b>Item Cost($)</b></td>
                        </tr>";
                    foreach ($lines as $line){
                        $camp_code = substr($line, 0,strpos($line,':'));                    
                        $info = substr($line, strpos($line,':')+1);
                        $infoPieces = explode(" " , $info);
                        $processing_fee = $infoPieces[1] * 2;
                        $formatted_totalgross = number_format($infoPieces[2], 2, '.', '');
                        $newtable .= "<tr><td>$camp_code</td><td>$infoPieces[1]</td><td>$infoPieces[0]</td><td>$formatted_totalgross</td>
                            <td>$processing_fee</td><td>$infoPieces[3]</td><td>$infoPieces[4]</td></tr>";
                        $arevorders += $infoPieces[1];
                        $cartorders += $infoPieces[0];
                        $totalamount += $infoPieces[2];
                        $total_fee +=$processing_fee;
                        $total_postage += $infoPieces[3];
                        $total_cost += $infoPieces[4];
                    }
                    $formatted_totalamount = number_format($totalamount, 2, '.', '');
                    $newtable .= "<tr><td></td><td>$arevorders</td><td>$cartorders</td><td>$formatted_totalamount</td>
                        <td>$total_fee</td><td>$total_postage</td><td>$total_cost</td>
                        </tr></table>";
                        ?>
                    <td><?php print $row['id']; ?></td>
                    <td><a href="<?php print $base_url; ?>/order_export/reports/<?php print $row['filename']; ?>"><?php print $row['filename']; ?></a></td>
                    <td><?php print $row['create_time']; ?></td>
                    <td><?php print $newtable;?></td>
                    <td><form action="<?php echo $base_url; ?>/admin/store/orders/export" accept-charset="UTF-8" method="post" id="core101-export-order-file-form">
                            <input type="submit" name="delete" id="delete<?php print $row['id'] ?>"  value="Delete File <?php print $row['id'] ?>" class="form-submit">
                        </form></td>
                </tr>
            <?php endforeach; ?>
        <?php endif; ?>
    </tbody>
</table>